<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/classes/BaseModule.class.php, $Author:  $ */

/**
 * Base class for all module classes.
 *
 * @package module
 *
 */
class BaseModule extends Object
{
    /**
     * Module's name
     *
     * @var string
     */
    var $_sName = '';

    /**
     * Module's items
     *
     * @var array
     */
    var $_aItems = array();

    /**
     * Module's services
     *
     * @var unknown_type
     */
    var $_aServices = array();

    /**
     * Module components.
     *
     * @var unknown_type
     */
    var $_aComponents = array();

    /**
     * How long cache file can live (in seconds)
     *
     * @var integer
     */
    var $_iTime = 1800;

    /**
     * Return $sName
     *
     * @return unknown
     */
    function getName()
    {
        return $this->_sName;
    }

    /**
     * Get module's item-class
     *
     * @param string $sName item name
     * @return DbItem
     */
    function &getItem($sName)
    {
        if (isset($this->_aItems[$sName]))
        {
            $oItem = & App::newObject('Mod_'.$this->_sName.'_'.$this->_aItems[$sName]);
            return $oItem;
        }
        else
        {
            trigger_error('No item "'.$sName.'" in module "'.$this->_sName.'".', E_USER_ERROR);
        }
    }

    /**
     * Enter description here...
     *
     * @param string $sName
     */
    function &getService($sName)
    {
        if (isset($this->_aServices[$sName]))
        {
            $oItem = & App::newObject('Mod_'.$this->_sName.'_'.$this->_aServices[$sName], $this);
            return $oItem;
        }
        else
        {
            trigger_error('No service "'.$sName.'" in module "'.$this->_sName.'".', E_USER_ERROR);
        }
    }

    /**
     * Get module's component
     *
     * @param string $sName    component name
     * @param array  $aParams  component params
     * @return BaseComponent
     */
    function &getComponent($sName, $aParams)
    {
        if (isset($this->_aComponents[$sName]))
        {
            $oComponent = & App::newObject('Mod_'.$this->_sName.'_'.$this->_aComponents[$sName], $this, $aParams);
            return $oComponent;
        }
        else
        {
            trigger_error('No component "'.$sName.'" in module "'.$this->_sName.'".', E_USER_ERROR);
        }
    }

    /**
     * Is module active
     *
     * @return true
     */
    function isActive()
    {
        return true;
    }

    // ========== WORK WITH SESSION ==========
    /**
     * Set session variable
     *
     * @param string $sName
     * @param mixed  $mValue
     */
    function setSessVar($sName, $mValue)
    {
        $_SESSION['Mod_'.$this->getName()][$sName] = $mValue;
    }

    /**
     * Unset session variable
     *
     * @param string $sName
     */
    function unsetSessVar($sName)
    {
        if (isset($_SESSION['Mod_'.$this->getName()][$sName]))
        {
            unset($_SESSION['Mod_'.$this->getName()][$sName]);
        }
    }

    /** Get session variable
     *
     * @param string $sName    session variable name
     * @param mixed  $mDefault default value
     * @return mixed
     */
    function getSessVar($sName, $mDefault='')
    {
        return isset($_SESSION['Mod_'.$this->getName()][$sName]) ? $_SESSION['Mod_'.$this->getName()][$sName] : $mDefault;
    }

    /** Is session variable defined
     *
     * @param string $sName
     * @return bool
     */
    function hasSessVar($sName)
    {
        return isset($_SESSION['Mod_'.$this->getName()][$sName]);
    }

    /** Get module's template.
     *
     * @return Template
     */
    function &getTemplate()
    {
        static $oTpl = null;

        if (null == $oTpl)
        {
            App::loadClass('Template');
            $oTpl = new Template();
            $sMainTplPath = $oTpl->template_dir;

          	$oTpl->template_dir = App::getParam('path').'design/templates/'. App::getLayout() .'/_modules/'.$this->_sName.'';
            //$oTpl->compile_dir = App::getParam('path').'file/templates_c/'.$this->_sName.'';
            $oTpl->compile_dir = App::getParam('path').'file/templates_c';
            /*
            if (!file_exists($oTpl->compile_dir))
            {
                 mkdir($oTpl->compile_dir);
            }
            */
  	      //  $oTpl->use_sub_dirs  = false;
  	        $oTpl->force_compile = false;

  	        if ( !compile_check )
  	        {
  	        	$oTpl->compile_check = false;
  	        }

            $oUrl = &App::getUrl();
            $oTpl->assign(array(
                'sUrl'              => $oUrl->getUrl(),
                'sMainTemplatePath' => $sMainTplPath,
                'sRootPath'         => $sMainTplPath,
            ));
        }

        return $oTpl;
    }

    /**
     * Loads module-related files
     *
     * @param string $sModule
     * @param string $sLang
     */
    function loadFiles($sModule, $sLang = 'en')
    {
        //load settings file
        $sSettFile = App::getParam('path').'include/modules/'.$sModule.'/include/module.sett.php';
        if (file_exists($sSettFile))
        {
            require_once($sSettFile);
        }
    }

    /**
     * Clear data and html cache
     *
     * @param array $aComponents
     * @param array $aServMethods
     */
    function clearCache($aComponents, $aServMethods = array())
    {
        foreach ($aComponents as $sComponent)
        {
            $sComponent = 'component'.$sComponent;
            $this->_clearDir($this->getHtmlCacheDir($sComponent));
        }
        foreach ($aServMethods as $sServMethod)
        {
            $sService = 'service'.$sServMethod;
            list($sService, $sMethod) = explode('::', $sService);
            $this->_clearDir($this->getDataCacheDir($sService, $sMethod));
        }
    }

    /**
     * Clearing all contents in directory
     *
     * @param string $sDir
     */
    function _clearDir($sDir)
    {
        if (file_exists($sDir))
        {
            $oDir = dir($sDir);
            while (false !== ($sFile = $oDir->read()))
            {
                if (!is_dir($sFile))
                {
                    unlink($sDir.$sFile);
                }
            }
        }
    }

    /**
     * Returns directory with html cache for this component
     *
     * @param string $sComponent
     * @return string
     */
    function getHtmlCacheDir($sComponent)
    {
        $sComponent = strtolower($sComponent);
        $sComponent = str_replace('mod_'.strtolower($this->_sName).'_', '', $sComponent);
        return App::getParam('path').'cache/modules/'.strtolower($this->_sName).'/'.$sComponent.'/';
    }

    /**
     * Returns directory with data cache for this service method
     *
     * @param string $sService
     * @param string $sMethod
     * @return string
     */
    function getDataCacheDir($sService, $sMethod)
    {
        $sService = strtolower($sService);
        $sService = str_replace('mod_'.strtolower($this->_sName).'_', '', $sService);
        $sMethod = strtolower($sMethod);
        return App::getParam('path').'file/cache/modules/'.strtolower($this->_sName).'/'.$sService.'/'.$sMethod.'/';
    }
}
?>